package a10_动态规划;

import java.util.Arrays;

/**
 * <p>
 * a13_整数拆分复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/2/12
 */
public class a13_分割等和子集复习1 {
    //dp[j]:容量为j的背包最多存放的价值

    //此时的重量和价值都是nums[i]

    public boolean canPartition(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        if(sum % 2 != 0){
            return false;
        }

        int target = sum / 2;

        int[] dp = new int[target + 1];
        dp[0] = 0;

        for (int i = 0; i < nums.length; i++) {
            for (int j = target; j >= nums[i]; j--) {
                dp[j] = Math.max(dp[j],dp[j-nums[i]] + nums[i]);
            }
        }

        return dp[target] == target;
    }
}
